package com.ruoyi.js.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class IdcardUtils {

    /**
     * 通过身份证号码获取出生日期、性别、年龄
     *
     * @param idcard
     * @return 返回的出生日期格式：1990-01-01 性别格式：女，男
     */
    public static Map<String, String> getBirAgeSex(String idcard) {
        String birthday = "";
        String age = "";
        String sex = "";

        int year = Calendar.getInstance().get(Calendar.YEAR);
        char[] number = idcard.toCharArray();
        boolean flag = true;
        if (number.length == 15) {
            for (int x = 0; x < number.length; x++) {
                if (!flag)
                    return new HashMap<String, String>();
                flag = Character.isDigit(number[x]);
            }
        } else if (number.length == 18) {
            for (int x = 0; x < number.length - 1; x++) {
                if (!flag)
                    return new HashMap<String, String>();
                flag = Character.isDigit(number[x]);
            }
        }
        if (flag && idcard.length() == 15) {
            birthday = "19" + idcard.substring(6, 8) + "-" + idcard.substring(8, 10) + "-"
                    + idcard.substring(10, 12);
            sex = Integer.parseInt(idcard.substring(idcard.length() - 3, idcard.length()))
                    % 2 == 0 ? "女" : "男";
            age = (year - Integer.parseInt("19" + idcard.substring(6, 8))) + "";
        } else if (flag && idcard.length() == 18) {
            birthday = idcard.substring(6, 10) + "-" + idcard.substring(10, 12) + "-"
                    + idcard.substring(12, 14);
            sex = Integer.parseInt(idcard.substring(idcard.length() - 4, idcard.length() - 1))
                    % 2 == 0 ? "女" : "男";
            age = (year - Integer.parseInt(idcard.substring(6, 10))) + "";
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("birthday", birthday);
        map.put("age", age);
        map.put("sex", sex);
        return map;
    }

    public static String getBirthday(String idcard) {
        Map<String, String> map = getBirAgeSex(idcard);
        String birthday = map.get("birthday");
        return birthday;
    }

    public static String getAge(String idcard) {
        Map<String, String> map = getBirAgeSex(idcard);
        String age = map.get("age");
        return age;
    }

    public static String getSex(String idcard) {
        Map<String, String> map = getBirAgeSex(idcard);
        String sex = map.get("sex");
        return sex;
    }


    /**
     * 身份证号校验 （支持18位）
     */
    public static boolean validIdcard(String idcard) {
        if (!idcard.matches("\\d{17}(\\d|x|X)$")) {
            return false;
        }
        Date d = new Date();
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        int year = Integer.parseInt(df.format(d));

        // 7-10位是出生年份，范围应该在1900-当前年份之间
        if (Integer.parseInt(idcard.substring(6, 10)) < 1900 || Integer.parseInt(idcard.substring(6, 10)) > year) {
            return false;
        }

        // 11-12位代表出生月份，范围应该在01-12之间
        if (Integer.parseInt(idcard.substring(10, 12)) < 1 || Integer.parseInt(idcard.substring(10, 12)) > 12) {
            return false;
        }

        // 13-14位是出生日期，范围应该在01-31之间
        if (Integer.parseInt(idcard.substring(12, 14)) < 1 || Integer.parseInt(idcard.substring(12, 14)) > 31) {
            return false;
        }

        // 校验第18位
        char[] id = idcard.toCharArray();
        int sum = 0;
        int w[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
        char[] ch = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3',
                '2' };
        for (int i = 0; i < id.length - 1; i++) {
            sum += (id[i] - '0') * w[i];
        }
        int c = sum % 11;
        char code = ch[c];
        char last = id[id.length - 1];
        last = last == 'x' ? 'X' : last;
        return last == code;

    }

}
